//
//  ChatGiftCell.m
//  beautyAccompany
//
//  Created by xiaocankeji on 15/11/13.
//  Copyright © 2015年 xiaocankeji. All rights reserved.
//

#import "ChatGiftCell.h"

@implementation ChatGiftCell

#define  giftWith 210


- (instancetype)initWithStyle:(UITableViewCellStyle)style
              reuseIdentifier:(NSString *)reuseIdentifier
                        model:(id<IMessageModel>)model
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self _setupSubviewsWithSender:model.isSender
                               model:model];
        self.model = model;
    }
    
    return self;
}
#pragma mark - setup subviews

- (void)_setupSubviewsWithSender:(BOOL)isSender
                         model:(id<IMessageModel>)model
{
    self.backgroundColor = [UIColor clearColor];
    _avatarView = [[UIImageView alloc] init];
    _avatarView.backgroundColor = [UIColor clearColor];
    _avatarView.clipsToBounds = YES;
    _avatarView.userInteractionEnabled = YES;
    [self.contentView addSubview:_avatarView];
    
    self.giftBackgroudImageView = [[UIImageView alloc] init];
    _giftBackgroudImageView.userInteractionEnabled = NO;
    [self.contentView addSubview:_giftBackgroudImageView];
    self.giftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 60, 60)];
    [_giftBackgroudImageView addSubview:_giftImageView];
    
    self.giftNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 13, 100, 20)];
    _giftNameLabel.font = kFont(16);
    _giftNameLabel.textColor = APPCOLOR(1);
    [_giftBackgroudImageView addSubview:_giftNameLabel];
    
    self.giftInfoLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 40, 140, 20)];
    _giftInfoLabel.font = kFont(14);
    _giftInfoLabel.adjustsFontSizeToFitWidth = YES;
    _giftInfoLabel.textColor = RGBA(84, 85, 86, 1);
    [_giftBackgroudImageView addSubview:_giftInfoLabel];
   
    if (!isSender) {
        _avatarView.frame = CGRectMake(10, 5, 40, 40);
        _avatarView.layer.cornerRadius = 20;
        _giftBackgroudImageView.frame = CGRectMake(60, 5, giftWith, 80);
        _giftBackgroudImageView.image = [[UIImage imageNamed:@"chat_gift_receiver_bg"] stretchableImageWithLeftCapWidth:15 topCapHeight:15];
    }else{
        _avatarView.frame = CGRectMake(DeviceWidth - 50, 5, 40, 40);
        _avatarView.layer.cornerRadius = 20;
        _giftBackgroudImageView.frame = CGRectMake(DeviceWidth - giftWith-60, 5, giftWith, 80);
         _giftBackgroudImageView.image = [[UIImage imageNamed:@"chat_gift_sender_bg"] stretchableImageWithLeftCapWidth:15 topCapHeight:15];
    }
    
    
    UITapGestureRecognizer *tapRecognizer2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(avatarViewTapAction:)];
    [_avatarView addGestureRecognizer:tapRecognizer2];
}
- (void)setModel:(id<IMessageModel>)model
{
    if (model.avatarURLPath) {
       
        [self.avatarView sd_setImageWithURL:[NSURL URLWithString:model.avatarURLPath] placeholderImage:model.avatarImage];
       
    } else {
        self.avatarView.image = model.avatarImage;
    }
     NSDictionary *dic =  model.message.ext;
    [self.giftImageView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"giftImg"]] placeholderImage:[UIImage imageNamed:@"chat_gift_icon"]];
    self.giftNameLabel.text = [dic objectForKey:@"giftName"];
    self.giftInfoLabel.text = [dic objectForKey:@"giftInfo"];
    
}

- (void)avatarViewTapAction:(UITapGestureRecognizer *)tapRecognizer
{
    if ([_delegate respondsToSelector:@selector(clickGitfAvatarViewMode:)]) {
        [_delegate clickGitfAvatarViewMode:_model];
    }
    
//    if ([_delegate respondsToSelector:@selector(avatarViewSelcted:)]) {
//        [_delegate avatarViewSelcted:_model];
//    }
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
